home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / dowc.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  1KB  |  72 lines

  1. #include "kiss.h"
  2.  
  3. static void wordcount (FILE *f, char *name, int *tlines, int *twords,
  4.                int *tchars)
  5. {
  6.     register int
  7.     lastchar = -1,
  8.     ch,
  9.     lines = 0,
  10.     chars = 0,
  11.     words = 0;
  12.     
  13.     while (1)
  14.     {
  15.     ch = fgetc (f);
  16.     if (feof (f))
  17.         break;
  18.  
  19.     chars++;
  20.     if (ch == '\n')
  21.         lines++;
  22.  
  23.     if (lastchar != -1)
  24.         if (isalnum (ch) && ! isalnum (lastchar))
  25.         words++;
  26.  
  27.     lastchar = ch;
  28.     }
  29.  
  30.     printf ("%10d %10d %10d %s\n",
  31.         lines, words, chars,
  32.         name ? name : "");
  33.  
  34.     *tlines += lines;
  35.     *twords += words;
  36.     *tchars += chars;
  37. }
  38.  
  39. int dowc (Stringstack s)
  40. {
  41.     register int
  42.     ret = 0,
  43.     i;
  44.     int
  45.     totlines = 0,
  46.     totwords = 0,
  47.     totchars = 0;
  48.     FILE
  49.     *f;
  50.     
  51.     if (getopt (s.nstr, s.str, "h") != -1)
  52.     error ("Bad commandline.\n"
  53.            "Usage: %s [file(s)]\n", progname);
  54.  
  55.     if (s.nstr == 1)
  56.     wordcount (stdin, NULL, &totlines, &totwords, &totchars);
  57.  
  58.     for (i = 1; i < s.nstr; i++)
  59.     {
  60.     if (! (f = fopen (s.str [i], "r")) )
  61.         ret += warning ("cannot open \"%s\" for reading");
  62.     else
  63.         wordcount (f, s.str [i], &totlines, &totwords, &totchars);
  64.     }
  65.  
  66.     if (s.nstr > 2)
  67.     printf ("%10d %10d %10d total\n",
  68.         totlines, totwords, totchars);
  69.     
  70.     return (ret);
  71. }
  72.